package org.java.db;

import org.junit.Test;

import java.sql.*;

/**
 * jdbc测试增删改查
 * Created by Administrator on 2016/11/21.
 */
public class JdbcTest {

    @Test
    public void add(){
        Connection con = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/shiro?useSSL=true", "root", "root");
            ps = con.prepareStatement("insert into users values('test1','123456', null)");
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void query() {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/shiro?useSSL=true", "root", "root");
            ps = con.prepareStatement("select * from users");
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                System.out.println("username = " + rs.getString("username"));
            }
        } catch (Exception e) {
            System.out.print("MYSQL ERROR:" + e.getMessage());
        } finally {
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
